<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\User;
use app\api\model\UserAddress;
use app\common\model\ScoreLog;
use app\api\model\SignLog;

/**
 * 个人中心
 */
class Personage extends Api
{
    
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    
    
    /**
     * 个人信息 
     */
    public function personage() {
        $userId = $this->auth->id;
        $info = User::field('username,avatar,score')->where('id',$userId)->find();
        if ($info['avatar']) {
            $info['avatar'] = 'https://' . $_SERVER['HTTP_HOST'] . $info['avatar'];
        } else {
            $info['avatar'] = 'https://' . $_SERVER['HTTP_HOST'] . '/assets/img/avatar.png';
        }
        $this->success('获取成功',$info);
    }
    
    /**
     *  签到页面数据
     */ 
    public function sign_info() {
        $userId = $this->auth->id;
        $yue = $this->request->get('yue');
        $info = User::field('username,avatar,check')->where('id',$userId)->find();
        $prevDay = strtotime(date('Y-m-d', strtotime('-1 day')));
        $startTime = strtotime(date('Y-m-d 00:00:00'));
        $endTime = strtotime(date('Y-m-d 23:59:59'));
        SignLog::where('uid',$userId)->find();
        if ($prevDay) {
            
        }
        
        
        
    }
    
    
    
    
    
    
    
    
    /**
     * 用户地址
     */ 
    public function addressList()
    {
        $userId = $this->auth->id;
        if (!$userId) {
            $this->error('参数错误');
        }
        $list = UserAddress::where('uid',$userId)->select();
        $this->success('获取成功',$list);
    }
    
    
    /**
     * 修改地址
     */ 
    public function exitAddress()
    {
        $id = $this->request->post('id');
        $data = $this->request->post();
        $address = $this->request->post('address');
        $phone = $this->request->post('phone');
        $userId = $this->auth->id;
        $top = $this->request->post('top');
        if (!$userId) {
            $this->error('参数错误');
        }
        $data['uid'] = $userId;
        if ($id) {
            if($data['top'] == 1){
                $addressInfo = UserAddress::where('uid',$userId)->where('top',1)->find();
                if($addressInfo){
                   $addressInfo->top = 2;
                   $addressInfo->save(); 
                }
            }
            $res = UserAddress::where(['id'=>$id,'uid'=>$userId])->find();
            if ($res) {
                $ret = UserAddress::where(['id'=>$id,'uid'=>$userId])->update($data);
            } else {
                $this->error('地址不存在，或已删除');
            }
           
        } else {
            if($data['top'] == 1){
                $addressInfo = UserAddress::where('uid',$userId)->where('top',1)->find();
                if($addressInfo){
                   $addressInfo->top = 2;
                   $addressInfo->save(); 
                }
            }
            $ret = UserAddress::insert($data);
        }
        if ($ret) {
            $this->success('提交成功');
        } else {
            $this->error('网络错误');
        }
        
    }
    
    /**
     * 删除地址
     */
    public function delete(){
        $id = $this->request->post('id');
        $userId = $this->auth->id;
        if (!$userId) {
            $this->error('参数错误');
        }
        $ret = UserAddress::where(['id'=>$id,'uid'=>$userId])->delete();
        
        if ($ret) {
            $this->success('删除成功');
        } else {
            $this->error('网络错误');
        }
    }
    
    
    /**
     * 设置默认地址
     */
    public function address() {
        $userId = $this->auth->id;
        if (!$userId) {
            $this->error('参数错误');
        }
        $ret = UserAddress::where(['top'=>1,'uid'=>$userId])->find();
        $this->success('获取成功',$ret);
        
    }
    
    
    /**
     * 设置默认地址
     */
    public function top() {
        $id = $this->request->post('id');
        $userId = $this->auth->id;
        if (!$userId) {
            $this->error('参数错误');
        }
        $addressInfo = UserAddress::where('uid',$userId)->where('top',1)->find();
        if($addressInfo){
           $addressInfo->top = 2;
           $addressInfo->save(); 
        }
        $ret = UserAddress::where(['id'=>$id])->find();
        $ret->top = 1;
        $ret->save();
        if ($ret) {
            $this->success('设置成功');
        } else {
            $this->error('网络错误');
        }
    }


    #地址回显
    public function addressShow(){
        $id = $this->request->post('id');
        $userId = $this->auth->id;
         if (!$userId) {
            $this->error('请先登录！');
        }
        $addressInfo = UserAddress::where('id',$id)->find();
        $this->success('获取成功',$addressInfo);
    }
    
    
    
    
    /**
     * 收支明细
     */
    public function earning_log(){
        $userId = $this->auth->id;
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $page = empty($page) ? 1 : $page;
        $limit = empty($limit) ? 10 : $limit;
        $info = User::field('username,avatar,score')->where('id',$userId)->find();
        $info['avatar'] = 'https://' . $_SERVER['HTTP_HOST'] . $info['avatar'];
        $log = ScoreLog::where('user_id',$userId)->page($page,$limit)->select();
        foreach ($log as $key => $val) {
            $log[$key]['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
        }
        $data = [
            'info' =>$info,
            'log' =>$log,
            'count' => count($log)
            ];
        $this->success('获取成功',$data);
        
        
        
        
        
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}